package com.jkl.service.dsRelationship;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.jkl.dao.drTable.DrTableMapper;
import com.jkl.dao.drTableDetail.DrTableDetailMapper;
import com.jkl.dao.dsRelationship.DsRelationshipMapper;
import com.jkl.entity.drTable.DrTable;
import com.jkl.entity.drTableDetail.DrTableDetail;
import com.jkl.entity.dsRelationship.DsRelationship;

@Service
public class DsRelationshipService {

	@Autowired
	DrTableMapper drTableMapper;
	@Autowired
	DrTableDetailMapper drTableDetailMapper;
	@Autowired
	DsRelationshipMapper dsRelationshipMapper;

	@Transactional(rollbackFor = Exception.class)
	public void handleDataArchive(DrTable originalTable, int opCode) {
		Map<String, String> tableDetailMap = tableDetailMap(originalTable.getRowId());
		List<DsRelationship> relationshipList = dsRelationshipMapper
				.queryRelationshipsByIndexCodes(tableDetailMap.keySet());
		Map<String, List<ImmutableTriple<String, Integer, String>>> archiveMap = targetDataMap(relationshipList,
				tableDetailMap);
		doAction(originalTable, archiveMap, opCode);
	}

	void doAction(DrTable originalTable, Map<String, List<ImmutableTriple<String, Integer, String>>> archiveMap,
			Integer code) {
		if (null == archiveMap || archiveMap.isEmpty()) {
			return;
		}
		for (Map.Entry<String, List<ImmutableTriple<String, Integer, String>>> entry : archiveMap.entrySet()) {
			if (Ints.compare(code, 1) == 0) {
				dsRelationshipMapper.doArchive(entry.getKey(), originalTable.getPeriod(), originalTable.getDeptId(),
						entry.getValue());
			}
			if (Ints.compare(code, 2) == 0) {
				dsRelationshipMapper.cancelArchive(entry.getKey(), originalTable.getPeriod(), originalTable.getDeptId(),
						entry.getValue());
			}
		}
	}

	public int opCode(DrTable originalDrTable, String workflowCode) {
		String orginalCode = Optional.ofNullable(originalDrTable).map(DrTable::getWorkflowCode)
				.orElseThrow(() -> new RuntimeException());
		if (StringUtils.equals(workflowCode, "15")) {// 归档操作
			return 1;
		} else if (StringUtils.equals(workflowCode, "12") && StringUtils.equals(orginalCode, "15")) { // 退档
			return 2;
		}
		return 0;
	}

	public DrTable targetDrTableById(Integer tableId) {
		return Optional.ofNullable(drTableMapper.selectByPrimaryKey(new DrTable().setRowId(tableId)))
				.orElseThrow(() -> new RuntimeException());
	}

	Map<String, String> tableDetailMap(Integer tableId) {
		List<DrTableDetail> tableDetails = drTableDetailMapper
				.queryDrTableDetail(new DrTableDetail().setTableId(tableId));
		return tableDetails.stream().collect(Collectors.toMap(DrTableDetail::getIndexCode, DrTableDetail::getValue));
	}

	Map<String, List<ImmutableTriple<String, Integer, String>>> targetDataMap(List<DsRelationship> relationshipList,
			Map<String, String> tableDetailMap) {
		if (CollectionUtils.isEmpty(relationshipList)) {
			return Collections.emptyMap();
		}
		return relationshipList.stream().collect(Collectors.toMap(DsRelationship::getDsTableName, item -> {
			List<ImmutableTriple<String, Integer, String>> dataList = Lists.newLinkedList();
			dataList.add(ImmutableTriple.of(item.getDsColumnName(), item.getDsType(),
					tableDetailMap.get(item.getIndexCode())));
			return dataList;
		}, (oldVal, newVal) -> {
			newVal.addAll(oldVal);
			return newVal;
		})

		);
	}
}
